package jrdcom.com.jrdmovie.Fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import jrdcom.com.jrdmovie.Adapter.JrdRecyleAdapter;
import jrdcom.com.jrdmovie.JrdHttpMethod.Beans.JrdSubjectBean;
import jrdcom.com.jrdmovie.Present.JrdTopMovieConstract;
import jrdcom.com.jrdmovie.Present.JrdTopPresent;
import jrdcom.com.jrdmovie.R;

/**
 * Created by longcheng on 2017/3/27.
 */

public class TopMovieFragment extends Fragment implements JrdTopMovieConstract.JrdTopMoveView{
    public static final String TAG = TopMovieFragment.class.getSimpleName();//this.getClass().getSimpleName();
    private RecyclerView jrdRecyleView;
    private List<JrdSubjectBean> jrdSubjectBeanList;
    private JrdRecyleAdapter jrdRecyleAdapter;
    JrdTopMovieConstract.JrdTopMoviePresent jrdTopMoviePresent;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.layout_top_movie,container,false);
        jrdRecyleView =(RecyclerView)v.findViewById(R.id.jrd_top_movie_list);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initRecyleView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresent();
        if(null == savedInstanceState){
            jrdTopMoviePresent.getMovieList();
        }
        jrdSubjectBeanList = new ArrayList<>();
        jrdRecyleAdapter = new JrdRecyleAdapter(getContext());//new JrdRecyleAdapter(jrdSubjectBeanList, getContext());
    }

    private void initRecyleView(){
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        jrdRecyleView.setLayoutManager(linearLayoutManager);
        jrdRecyleView.setAdapter(jrdRecyleAdapter);
        jrdRecyleView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //
                int totalCount = linearLayoutManager.getItemCount();
                int lastPosition = linearLayoutManager.findLastVisibleItemPosition();
                if(lastPosition>=(totalCount - 3) && dy >0){
                    jrdTopMoviePresent.getMovieExt();
                }
            }
        });
        jrdRecyleAdapter.setOnItemClickListener(new JrdRecyleAdapter.JrdOnItemClick() {
            @Override
            public void onClick(View view, int position) {
                //进入movie detail屏幕
                JrdMoveDetailFragment topMovieFragment= new JrdMoveDetailFragment();
                //传入参数
                JrdSubjectBean jrdSubjectBean = jrdTopMoviePresent.getMovieByPosition(position);
                Bundle bundle = new Bundle();
                bundle.putString("MovieId",jrdSubjectBean.getId());
                topMovieFragment.setArguments(bundle);
                getFragmentManager()
                        .beginTransaction()
                        .replace(R.id.activity_main_RL, topMovieFragment)
                        .addToBackStack(JrdMoveDetailFragment.TAG)
                        .commit();

            }
        });
        //jrdRecyleView.
    }

    /*MVP 模式，定义present*/
    private void initPresent(){
        jrdTopMoviePresent = new JrdTopPresent(getContext());
        jrdTopMoviePresent.setView(this);
    }

    /*private void loadData(){
        jrdTopMoviePresent.getMovie();
    }*/

    @Override
    public void renderView(List<JrdSubjectBean> subjectBeanList) {
        jrdRecyleAdapter.setArray(subjectBeanList);
    }
}
